package goutil

// 初始化工作
type InitWorkType func() (err error)
type StartWorkType func()
type StopWorkType func()
type AfterWorkType func() (err error)

var (
	InitWork    InitWorkType
	StartWork   StartWorkType
	StopWork    StopWorkType
	AfterWork   AfterWorkType
	AppName     string
	Working     bool = false
	CanWork     bool = false
	TradingDays []string
	TradingDay  string
)

/*

func InitTradeEnv() {
	TradingDays = ReadCalendar()
	for i, day := range TradingDays {
		curDate := time.Now().Format("20060102")
		cmp := strings.Compare(day, curDate)
		if cmp < 0 {
			continue
		}
		TradingDay = day
		os.Setenv("TradingDay", day)
		td_1 := TradingDays[i-1] //前一交易日
		td1 := TradingDays[i+1]  //后一交易日
		now := time.Now()
		hour := time.Now().Hour()
		minute := time.Now().Minute()
		ns := now.Format("20060102")
		ns_1 := now.AddDate(0, 0, -1).Format("20060102")
		ns1 := now.AddDate(0, 0, 1).Format("20060102")
		ns2 := now.AddDate(0, 0, 2).Format("20060102")
		ns3 := now.AddDate(0, 0, 3).Format("20060102")
		/0--2:30有交易的情况
		td_1 && (now || t2)有交易 ::


		//21:00:00---24:00:00可交易：今天、明天都是交易日或者今天是交易日明天不是交易日但第三日是交易日
		hasNightTrade := false
		//00:00:00---02:30:00可交易：昨天、今天都是交易日或者昨天是交易日明天不是交易日但第三日是交易日
		hasMidNightTrade := false
		hasNightTrade = (day == ns && ns1 == td1) || (day == ns || td1 == ns3)
		hasMidNightTrade = day == ns && ns_1 == td_1 || (day != ns && ns_1 == td_1 && ns2 == td1)
		//今天和昨天都是工作日
		// logrus.Info(hasMidNightTrade, day, hasNightTrade, ns, td1, ns1, hour)
		if hasMidNightTrade {
			CanWork = false
			if hour < 2 || (hour == 2 && minute < 30) {
				CanWork = true
			}
		}
		if hasNightTrade {
			if hour > 20 || hour == 20 && minute > 45 {
				CanWork = true
			}
		}

		if day == ns {
			if hour > 8 && hour < 15 {
				CanWork = true
			}
		}
		if (hour > 3 && hour < 8) || (hour > 15 && hour < 20) {
			CanWork = false
		}
		// Rds.Set("CanWork", CanWork, 0)
		break
	}
}
func Run() {
	// logrus.Info("run working now...")
	tradingDays := ReadCalendar()
	logrus.Info("开始工作...", AppName) // len(tradingDays)
	curDate := time.Now().Format("20060102")
	working := false
	for i, day := range tradingDays {
		cmp := strings.Compare(day, curDate)
		if cmp < 0 {
			continue
		}
		// for working {
		// 	time.Sleep(1 * time.Second)
		// }
		// logrus.Info(day)
		// Start()
		// 02:30前开启
		if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 02:30:00", day), time.Local); time.Now().Before(startTime) {
			logrus.Info(startTime, time.Now(), "cmp:", cmp)
			td_1 := tradingDays[i-1] //前一交易日
			// td1 := tradingDays[i+1]  //后一交易日
			now := time.Now()
			ns := now.Format("20200101")
			ns_1 := now.AddDate(0, 0, -1).Format("20200101")
			ns2 := now.AddDate(0, 0, 2).Format("20200101")

			//0--2:30有交易的情况
			td_1 && (now || t2)有交易 ::

			c1 := day == ns && ns_1 == td_1 //今天和昨天都是工作日
			c2 := ns_1 == td_1 && ns != day && ns2 == day
			if c1 || c2 {
				logrus.Info(day, "午夜作业开始：", AppName)
				if err := InitWork(); err != nil {
					logrus.Error("工作启动错误:", err)
					working = false
					panic(err)
				}
				StartWork()
				working = true
			}
		}
		// 8:45之前等待
		if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 08:45:00", day), time.Local); time.Now().Before(startTime) {
			logrus.Info("waiting for working start at ", startTime)
			if working {
				StopWork()
				working = false
			}
			time.Sleep(time.Until(startTime))
		} // 15:00前开启
		if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 15:00:00", day), time.Local); time.Now().Before(startTime) {
			logrus.Info(day, "启动作业：", AppName)
			if err := InitWork(); err != nil {
				logrus.Error("工作启动错误:", err)
				working = false
				panic(err)
			}
			StartWork()
			working = true

		}
		// 当日有夜盘(下一交易日在3天内)
		if strings.Compare(tradingDays[i+1], time.Now().AddDate(0, 0, 3).Format("20060102")) <= 0 {
			if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 20:45:00", day), time.Local); time.Now().Before(startTime) {
				logrus.Info("waiting for night working at ", startTime)
				if working {
					StopWork()
					working = false
				}
				time.Sleep(time.Until(startTime))
			}
			// 夜盘开启
			logrus.Info(day, "开始夜间作业：", AppName)
			if err := InitWork(); err != nil {
				logrus.Error("工作启动错误:", err)
				working = false
				panic(err)
			}
			StartWork()
			working = true
		}
		if !working {
			InitWork()
		}
	}
}

func RunAsSeven() {
	// logrus.Info("run working now...")
	tradingDays := ReadCalendar()
	logrus.Info("开始工作...", AppName) // len(tradingDays)
	curDate := time.Now().Format("20060102")
	working := false
	for i, day := range tradingDays {
		cmp := strings.Compare(day, curDate)
		if cmp < 0 {
			continue
		}
		// for working {
		// 	time.Sleep(1 * time.Second)
		// }
		// logrus.Info(day)
		// Start()
		// 02:30前开启
		if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 02:30:00", day), time.Local); time.Now().Before(startTime) {
			logrus.Info(startTime, time.Now(), "cmp:", cmp)
			td_1 := tradingDays[i-1] //前一交易日
			// td1 := tradingDays[i+1]  //后一交易日
			now := time.Now()
			ns := now.Format("20200101")
			ns_1 := now.AddDate(0, 0, -1).Format("20200101")
			ns2 := now.AddDate(0, 0, 2).Format("20200101")

			//0--2:30有交易的情况
			td_1 && (now || t2)有交易 ::

			c1 := day == ns && ns_1 == td_1 //今天和昨天都是工作日
			c2 := ns_1 == td_1 && ns != day && ns2 == day
			if c1 || c2 {
				logrus.Info(day, "午夜作业开始：", AppName)
				if err := InitWork(); err != nil {
					logrus.Error("工作启动错误:", err)
					working = false
					panic(err)
				}
				StartWork()
				working = true
			}
		}
		// 8:45之前等待
		if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 08:45:00", day), time.Local); time.Now().Before(startTime) {
			logrus.Info("waiting for working start at ", startTime)
			if working {
				StopWork()
				working = false
			}
			time.Sleep(time.Until(startTime))
		} // 15:00前开启
		if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 15:00:00", day), time.Local); time.Now().Before(startTime) {
			logrus.Info(day, "启动作业：", AppName)
			if err := InitWork(); err != nil {
				logrus.Error("工作启动错误:", err)
				working = false
				panic(err)
			}
			StartWork()
			working = true

		}
		// 当日有夜盘(下一交易日在3天内)
		if strings.Compare(tradingDays[i+1], time.Now().AddDate(0, 0, 3).Format("20060102")) <= 0 {
			if startTime, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%s 20:45:00", day), time.Local); time.Now().Before(startTime) {
				logrus.Info("waiting for night working at ", startTime)
				if working {
					StopWork()
					working = false
				}
				time.Sleep(time.Until(startTime))
			}
			// 夜盘开启
			logrus.Info(day, "开始夜间作业：", AppName)
			if err := InitWork(); err != nil {
				logrus.Error("工作启动错误:", err)
				working = false
				panic(err)
			}
			StartWork()
			working = true
		}
		if !working {
			InitWork()
		}
	}
}

//调用模板
func main() {
	util.Start = Start
	util.Stop = Stop
	go util.Run()
}

func Run724() {
	CanWork = false
	go InitTradeEnv()
	for !CanWork {
	}
	if CanWork {
		if InitWork != nil {
			InitWork()
		}

		if StartWork != nil {
			StartWork()
		}
	}
	tick := time.NewTicker(1 * time.Minute)
	for range tick.C {
		hhmm := time.Now().Local().Format("15:04")
		if CanWork { // 收盘后退出
			isContinue := false
			go func(working bool) bool {
				for working {
					isContinue = true
				}
				isContinue = false
				return true
			}(Working)
			if time.Now().Local().Minute()%15 == 0 { // 15 分钟显示一次
				logrus.Info(hhmm, " 工作中: ", AppName)
			}
			if !isContinue {
				nt := time.Now().Local()
				hour := nt.Hour()
				minute := nt.Minute()
				if hour == 2 && minute > 30 || hour == 15 { // 夜盘结束&当日收盘
					logrus.Info(AppName, ":开始休息： ", time.Now().Local())
					if AfterWork != nil {
						AfterWork()
					}
					if StopWork != nil {
						StopWork()
					}
				}

			}

		} else { // 定时连接
			if time.Now().Local().Minute()%15 == 0 { // 15 分钟显示一次
				logrus.Info(hhmm, " 结束工作:", AppName)
			}
			if hhmm == "08:40" || hhmm == "20:40" {
				logrus.Info(AppName, ":开始工作： ", time.Now().Local())
				if StartWork != nil {
					StartWork()
				}
			}
		}
	}
}

*/
